package com.henu.engineer.controller;

import com.henu.engineer.dao.entity.student.Student;
import com.henu.engineer.dao.mapper.student.StudentXmlMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author noexcept
 * @date 2021/7/23 0:36
 */
@RestController
public class LoginController {

    @Autowired
    private StudentXmlMapper studentXmlMapper;


    /**
     * 设置登录状态
     */
    @GetMapping("/login")
    public String Login(HttpSession session, HttpServletRequest request, String id, String pwd) {
        session.setAttribute("isLogged", true);
        session.setAttribute("account", id);
        session.setAttribute("rank","student");

        return "登录成功";

/*        String rank = request.getParameter("rank");
        if("teacher".equals(rank))
            return teacherLogin(session, id, pwd);
        else return studentLogin(session, id, pwd);*/
    }

    /**
     * 学生登录验证
     */
    private String studentLogin(HttpSession session, String id, String pwd) {
        Student student = studentXmlMapper.selectStudent(id);
        if (student == null) {
            return "账号不存在";
        }
        String s = studentXmlMapper.selectPwd(id);
        if (!s.equals(pwd))
            return "账号或密码错误！";

        session.setAttribute("isLogged", true);
        session.setAttribute("account", id);
        session.setAttribute("rank","student");

        return "登录成功";
    }

    /**
     * 老师登录验证
     */
    private String teacherLogin(HttpSession session, String id, String pwd) {

        // FIXME: 2021/7/23 添加验证条件

        session.setAttribute("isLogged", true);
        session.setAttribute("account", id);
        session.setAttribute("rank","teacher");
        return "登录成功";
    }
}
